package co.edu.uniandes.distribuidos.config;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class AppConfig {
	
	private static AppConfig singleton;
	
	private String contentAdvertisementDirectory;
	private String contentLoadDirectory;
	private String keystoreDirectory;

	static{
		singleton=new AppConfig();
	}
	
	private AppConfig(){
		init();
	}
	
	private void init(){
		//find config file
		 
	        File configFile = new File("config.ini");
	        if(configFile.exists() && configFile.isFile()){
	            System.out.println("Configuration file Found..");
	            readingConfigFile(configFile);            
	        }
	        else{
	        	System.out.println("Configuration file not found.."); 
	        	createConfigFile(); 
	        }
	}
	
	
	 private void readingConfigFile(File file)
	    {
	        try {
	            
	            BufferedReader in = new BufferedReader(new FileReader(file));
	            String str,path;
	            int index;
	            while((str=in.readLine())!=null){
	                if(str.startsWith("sharedDirectory")){
	                    index = str.indexOf("=");
	                    path = str.substring(index+1);
	                    File temp = new File(path);
	                    if(temp.exists()){
	                        contentAdvertisementDirectory = temp.getAbsolutePath();
	                        
	                    }else{
	                        createShareFolder(temp);
	                    }
	                }else if(str.startsWith("loadDirectory")){
	                    index = str.indexOf("=");
	                    path = str.substring(index+1);
	                    File temp = new File(path);
	                    if(temp.exists()){
	                        contentLoadDirectory = temp.getAbsolutePath();
	                        
	                    }else{
	                        createShareFolder(temp);
	                    }
	                }else if(str.startsWith("keystoreDirectory")){
	                	index = str.indexOf("=");
	                	path = str.substring(index+1);
	                	File temp = new File(path);
	                	if(temp.exists()){
	                		keystoreDirectory = temp.getAbsolutePath();
	                	}else{
	                		createShareFolder(temp);
	                	}
	                }
	            }
	            in.close();
	            
	        } catch (FileNotFoundException ex) {
	            ex.printStackTrace();
	        }catch(IOException e){
	            e.printStackTrace();
	        }
	    }
	 
	 
	    private void createShareFolder(File pathname) 
	    {
	       
	        if(pathname.mkdir()){
	        	if (pathname.getName().startsWith("sharedDirectory"))
	        		contentAdvertisementDirectory = pathname.getAbsolutePath();
	        	else if(pathname.getName().startsWith("keystoreDirectory")){
	        		keystoreDirectory = pathname.getAbsolutePath();
	        	}else
	        		contentLoadDirectory = pathname.getAbsolutePath();
	        }
	        
	    }
	    
	    private void createConfigFile()
	    {
	        File config = new File("config.ini");
	       
	        try {            
	            boolean success = config.createNewFile();
	            BufferedWriter out = new BufferedWriter(new FileWriter(config));
	            if(success){
	                out.write("sharedDirectory=" + "sharedDirectory");
	                out.newLine();
	                out.write("loadDirectory=" + "loadDirectory");
	                out.newLine();
	                out.write("keystoreDirectory=" + "keystoreDirectory");
	                out.newLine();
	                out.close();
	            }
	        } catch (IOException ex) {
	            ex.printStackTrace();
	        }
	        readingConfigFile(config);
	    }
	
	public static AppConfig getInstance(){
		return singleton;
	}
	
	public String getContentAdvertisementDirectory(){
		return contentAdvertisementDirectory;
	}

	public void setContentAdvertisementDirectory(
			String contentAdvertisementDirectory) {
		this.contentAdvertisementDirectory = contentAdvertisementDirectory;
	}

	public String getContentLoadDirectory() {
		return contentLoadDirectory;
	}

	public void setContentLoadDirectory(String contentLoadDirectory) {
		this.contentLoadDirectory = contentLoadDirectory;
	}

	public String getKeystoreDirectory() {
		return keystoreDirectory;
	}

	public void setKeystoreDirectory(String keystoreDirectory) {
		this.keystoreDirectory = keystoreDirectory;
	}
}
